<template>
  <div class="player-box-parent">
    <div class="player-box">
      <div class="player-box-right">
        <div class="right-top">
          <span class="audio-name">{{ getName() }}</span>
          <div class="right-top-menu">
            <div class="right-top-play" @click="play">
              <i class="doorIcon" v-if="!isPlay">&#xe672;</i>
              <i class="doorIcon" v-else>&#xe671;</i>
            </div>
          </div>
        </div>

        <div class="right-bottom" v-if="isPlay">
          <div v-if="isPlay" class="right-bottom-left">
            {{ realMusicTime }}
          </div>
          <div class="right-bottom-center">
            <van-slider v-model="percentage" @drag-end="change" @drag-start="dragStart" style="width: 100%" />
          </div>

          <div class="right-bottom-right">{{ totalMusicTime }}</div>
        </div>
      </div>
    </div>
  </div>
  <audio :src="urls + props.url" ref="audioRef" v-show="false" />
</template>

<script setup lang="ts">
// @ts-nocheck
import { Slider } from 'vant';
import { urls } from '@/utils/request/upload';
import { ref, onMounted, nextTick, reactive, computed } from 'vue';

const emit = defineEmits(['delAudioUrl']);
const audioRef = ref(null);
const isPlay = ref(false);
const realMusicTime = ref('00:00');
const totalMusicTime = ref('00:00');

const percentage = ref(0);
const move = ref(false);
// 进度条请使用样式穿透
const props = defineProps({
  url: {
    type: String,
  },
  name: {
    type: String,
  },

  //   audio: {
  //     type: Object,
  //   },
});

// default: {
//       type: 3,
//       url: '/369462942949445/audio/20230829/455210145833029.mp3',
//       description: '',
//       name: '桑原由気,長縄まりあ,高田憂希 - イシュカン・コミュニケーション.mp3',
//     },
const audio = reactive({
  type: 3,
  url: '/369462942949445/audio/20230829/455210145833029.mp3',
  description: '',
  name: '桑原由気,長縄まりあ,高田憂希 - イシュカン・コミュニケーション.mp3',
});

// 处理进度条
const getName = () => {
  if (!props.name) {
    return '';
  }
  let str = props.name.split('.mp3');
  // console.log(str);
  return str[0];
};

const del = () => {
  emit('delAudioUrl');
};
const handMusicBar = () => {
  let duration = audioRef.value.duration;
  let x = ((audioRef.value.currentTime / duration) * 100).toFixed(1);
  if (move.value) {
    return;
  }
  percentage.value = Number(x);

  // console.log(percentage.value, '当前播放百分比');
};

// 处理时间
const handlMusicTime = () => {
  //用秒数来显示当前播放进度
  let timeDisplay = Math.floor(audioRef.value.currentTime); //获取实时时间
  //分钟
  let minute = parseInt(timeDisplay / 60);
  if (minute < 10) {
    minute = '0' + minute;
  }
  //秒
  let second = Math.round(timeDisplay % 60);
  if (second < 10) {
    second = '0' + second;
  }
  realMusicTime.value = minute + ':' + second;
};

const watchMusicTime = () => {
  let music = audioRef.value;

  music.addEventListener(
    'timeupdate',
    () => {
      handlMusicTime();
      nextTick(() => {
        handMusicBar();
      });
    },
    false,
  );
  // 播放完毕
  music.addEventListener('ended', () => {
    //this.switchMusic(); // 自动播放
  });
  // 捕获音频文件已准备完毕
  // 当媒体文件可以播放的时候会触发oncanplay事件,也可以用oncanplay
  music.oncanplaythrough = () => {
    let time = audioRef.value.duration;
    //分钟
    let minutes = parseInt(time / 60);
    if (minutes < 10) {
      minutes = '0' + minutes;
    }
    //秒
    let seconds = Math.round(time % 60);
    if (seconds < 10) {
      seconds = '0' + seconds;
    }
    totalMusicTime.value = minutes + ':' + seconds;
  };
};
// 处理点击进度条事件
const handClickBar = (e) => {
  audioRef.value.currentTime = (percentage.value / 100) * audioRef.value.duration; // 计算播放时间 位置百分比*总时间
  nextTick(() => {
    audioRef.value.play();
    isPlay.value = true;
  });
};
const change = () => {
  move.value = false;
  handClickBar();
};
const dragStart = () => {
  move.value = true;
};
const play = () => {
  if (audioRef.value.paused) {
    audioRef.value.play();
    isPlay.value = true;
  } else {
    audioRef.value.pause();
    isPlay.value = false;
  }
};
onMounted(() => {
  watchMusicTime();
});
</script>

<style scoped lang="less">
.player-box-parent {
  display: flex;
  align-items: center;
  width: 100%;
  min-height: 48px;
  padding: 2px;
  background: linear-gradient(90deg, #fff4e1 0%, #fff4e1 100%);
}
.player-box {
  display: flex;
  align-items: center;
  width: 100%;
  min-height: 44px;
  border: 1px solid #e2d6c1;
  padding: 12px 10px;
  // box-shadow: 4px 0px 24px -12px rgba(208, 219, 229, 0.5);
}

.box {
  animation: scroll 5s linear infinite;
}
.player-box-right {
  display: flex;
  flex-direction: column;
  // margin-left: 12px;
  // flex: 1;
  width: 100%;
  // padding-right: 4px;
}
.right-top {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;

  .audio-name {
    max-width: 80%;
    font-family: PingFang SC, PingFang SC;
    font-weight: 400;
    font-size: 13px;
    color: #a2463c;
    line-height: 20px;
    text-align: left;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
.right-top-menu {
  display: flex;
  align-items: center;
  .doorIcon {
    font-size: 20px;
    line-height: 20px;
    color: rgba(162, 70, 60, 1);
  }
  .leftIcon {
    cursor: pointer;
    color: rgba(162, 70, 60, 1);
  }
}
.right-top-play {
  display: flex;
  margin-left: 16px;
  .doorIcon {
    color: rgba(162, 70, 60, 1);
    font-size: 20px;
    line-height: 20px;
    cursor: pointer;
  }
}
.time-box {
  margin-top: 6px;
  width: 100%;
  height: 15px;
  font-family: PingFang SC;
  font-size: 11px;
  font-weight: 400;
  line-height: 15px;
  letter-spacing: 0em;
  text-align: left;
}
.right-bottom {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  margin-top: 8px;
}
.right-bottom-left {
  opacity: 0.8;
  font-family: PingFang SC, PingFang SC;
  font-weight: 400;
  font-size: 12px;
  color: #a2463c;
  line-height: 19px;
}
.right-bottom-right {
  opacity: 0.8;
  font-family: PingFang SC, PingFang SC;
  font-weight: 400;
  font-size: 12px;
  color: #a2463c;
  line-height: 19px;
}
.right-bottom-center {
  flex: 1;
  margin: 0 8px;
  :deep(.van-slider) {
    // height: 10px;
    width: 100%;
    background: rgba(162, 70, 60, 0.2);
  }
  :deep(.van-slider__bar) {
    background: #a2463c;
  }
  :deep(.van-slider__button) {
    width: 6px;
    height: 6px;
    background: #a2463c;
  }
}
</style>
